/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:30 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class DiemdanhBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Diemdanh Này";
		private static string EX_FK_IBAIHOCID_INVALID="FK_iBaihocID không hợp lệ";
        private static string EX_FK_IKHOAHOCID_INVALID = "FK_iKhoahocID không hợp lệ";
        private static string EX_FK_ITRANGSLIDEID_INVALID = "FK_iTrangslideID không hợp lệ";
		private static string EX_FK_IHOCVIENID_INVALID="FK_iHocvienID không hợp lệ";
		private static string EX_DTHOIDIEMVAOLOP_INVALID="dThoidiemxem không hợp lệ";
		private static string EX_ITRANGTHAI_INVALID="iTrangthai không hợp lệ";
		private static string EX_ID_INVALID="FK_iBaihocID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Diemdanh Theo ID
        /// </summary>
        /// <param name="PK_iDiemdanhID">Int64:Diemdanh ID</param>
        /// <returns>DiemdanhEntity</returns>        
        public static DiemdanhEntity GetOne(Int64 PK_iDiemdanhID)
        {
            
			if(PK_iDiemdanhID<=0)
				throw new Exception(EX_ID_INVALID);
            return DiemdanhDAL.GetOne(PK_iDiemdanhID);
        }
        /// <summary>
        /// Lấy về List các Diemdanh
        /// </summary>
        /// <returns>List DiemdanhEntity:List Diemdanh Cần lấy</returns>
        public static List<DiemdanhEntity> GetAll()
        {
            return DiemdanhDAL.GetAll();
        }
        public static List<DiemdanhEntity> GetByFK_iHocvienID(Int64 FK_iHocvienID)
		{
			if(FK_iHocvienID<=0)
				throw new Exception(EX_FK_IHOCVIENID_INVALID);
			return DiemdanhDAL.GetByFK_iHocvienID(FK_iHocvienID);
		}
        public static List<DiemdanhEntity> GetByFK_iKhoahocID(Int64 FK_iKhoahocID)
        {
            if (FK_iKhoahocID <= 0)
                throw new Exception(EX_FK_IKHOAHOCID_INVALID);
            return DiemdanhDAL.GetByFK_iKhoahocID(FK_iKhoahocID);
        }
        public static List<DiemdanhEntity> GetByFK_iKhoahocIDAndFK_iHocvienID(Int64 FK_iKhoahocID, Int64 FK_iHocvienID)
        {
            if (FK_iKhoahocID <= 0)
                throw new Exception(EX_FK_IKHOAHOCID_INVALID);
            if (FK_iHocvienID <= 0)
                throw new Exception(EX_FK_IHOCVIENID_INVALID);
            return DiemdanhDAL.GetByFK_iKhoahocIDAndFK_iHocvienID(FK_iKhoahocID, FK_iHocvienID);
        }
        public static List<DiemdanhEntity> GetByFK_iKhoahocIDAndFK_iHocvienIDAndFK_iTrangslideID(Int64 FK_iKhoahocID, Int64 FK_iHocvienID, Int64 FK_iTrangslideID)
        {
            if (FK_iKhoahocID <= 0)
                throw new Exception(EX_FK_IKHOAHOCID_INVALID);
            if (FK_iHocvienID <= 0)
                throw new Exception(EX_FK_IHOCVIENID_INVALID);
            if (FK_iTrangslideID <= 0)
                throw new Exception(EX_FK_ITRANGSLIDEID_INVALID);
            return DiemdanhDAL.GetByFK_iKhoahocIDAndFK_iHocvienIDAndFK_iTrangslideID(FK_iKhoahocID, FK_iHocvienID, FK_iTrangslideID);
        }
        /// <summary>
        /// Kiểm tra và thêm mới Diemdanh
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Diemdanh Mới Thêm Vào</returns>
        public static Int32 Add(DiemdanhEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return DiemdanhDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Diemdanh
        /// </summary>
        /// <param name="entity">DiemdanhEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(DiemdanhEntity entity)
        {
            checkExist(entity.FK_iTrangslideID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return DiemdanhDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Diemdanh
        /// </summary>
        /// <param name="PK_iDiemdanhID">Int64 : FK_iBaihocID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 PK_iDiemdanhID)
        {
            checkExist(PK_iDiemdanhID);
            return DiemdanhDAL.Remove(PK_iDiemdanhID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 PK_iDiemdanhID)
        {
            DiemdanhEntity oDiemdanh = DiemdanhDAL.GetOne(PK_iDiemdanhID);
            if(oDiemdanh==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">DiemdanhEntity: entity</param>
        private static void checkLogic(DiemdanhEntity entity)
        {   
			if (entity.FK_iTrangslideID < 0)
				throw new Exception(EX_FK_IBAIHOCID_INVALID);
			if (entity.FK_iHocvienID < 0)
				throw new Exception(EX_FK_IHOCVIENID_INVALID);
			if (DateTime.Parse("1753-01-01")>entity.dThoidiemxem)
				throw new Exception(EX_DTHOIDIEMVAOLOP_INVALID);
			if (entity.iTrangthai < 0)
				throw new Exception(EX_ITRANGTHAI_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">DiemdanhEntity: DiemdanhEntity</param>
        private static void checkDuplicate(DiemdanhEntity entity,bool checkPK)
        {
            /* 
            Example
            List<DiemdanhEntity> list = DiemdanhDAL.GetAll();
            if (list.Exists(
                delegate(DiemdanhEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.FK_iBaihocID != entity.FK_iBaihocID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">DiemdanhEntity:entity</param>
        private static void checkFK(DiemdanhEntity entity)
        {            
			NguoidungEntity oNguoidung = NguoidungDAL.GetOne(entity.FK_iHocvienID);
			if (oNguoidung==null)
			{
				throw new Exception("Không tìm thấy :FK_iHocvienID");
			}
        }
        #endregion
    }
}
